package git.bankview.wl.report;

import java.util.*;
import java.sql.*;
import java.io.*;

import laya.semi.process.*;
import git.bankview.sl.report.ReportCommon;
import git.bankview.sl.report.ReportParameter;
import git.bankview.wl.bean.LogonBean;

import git.bankview.sl.db.*;

import org.apache.log4j.Logger;

public class WriteJasperParameterJournal
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(WriteJasperParameterJournal.class);
	
	private static final String SQL_APPENDER = "insert into dqschdparam (schdid, paramindex, paramname, paramvalue) values (?, ?, ?, ?)";
	
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		String[] vParamFieldName = oCtx.getActivityParameter().split(":");
		Integer oSchdid = (Integer)oCtx.getField("schdid");
		Vector oParamList = (Vector)oCtx.getField(vParamFieldName[0]);
		String[] vInputParamVal = (String[])oCtx.getField(vParamFieldName[1]);
		LogonBean oLogon = (LogonBean)oCtx.getField(vParamFieldName[2]);
		
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			PreparedStatement ps = oConn.prepareStatement(SQL_APPENDER);

			int paramIdx;
			for (paramIdx = 0; paramIdx < oParamList.size(); ++paramIdx)
			{
				ReportParameter param = (ReportParameter)oParamList.get(paramIdx);
				ps.setInt(1, oSchdid.intValue());
				ps.setInt(2, paramIdx);
				ps.setString(3, param.getName());
				ps.setString(4, vInputParamVal[paramIdx]);
				ps.addBatch();
			}
			
			// append internal parameters
			ps.setInt(1, oSchdid.intValue());
			ps.setInt(2, paramIdx++);
			ps.setString(3, ReportCommon.GRP_SCHD_USER);
			ps.setString(4, oLogon.getUsername());
			ps.addBatch();
			
			// commit
			ps.executeBatch();
		}
		finally
		{
			oConn.close();
		}
	}
}
